Instead of checking the condition at the top of the loop, as you saw with the Do While loop, you can also test the condition at the bottom of the loop. In this example, youll put the Do statement at the top of the loop on a line by itself. You can then have one or many statements. To terminate the loop, you specify Loop While <Condition>. As long as the condition evaluates to a True value, execution will go back to the top of the loop and execute the statements again. When you use this format of looping, youre guaranteed that the code inside the loop will be executed one time. Heres the syntax:
Do
<Statement Block>
Loop While <Cond>
The following example is from the Loop While command button.
Private Sub cmdLoopWhile_Click ()
Dim intNumber As Integer
Cls
intNumber = 1
Do
Print intNumber
intNumber = intNumber + 1 Loop While intNumber < 10 End Sub
Do Until
A Do Until loop is very similar to the Do While loop, except youre executing the loop until a condition becomes True. It all depends on what type of logic you need to use to check the loop condition. Heres the syntax:
Do Until <Cond>
<Statement Block>
Loop
The following example is from the Do Until command button.
Private Sub cmdUntil_Click ()
Dim intLoop As Integer